package com.example.dllo.my36kr.ui.activity.sonactivity;
import android.net.Uri;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.VideoView;
import com.example.dllo.my36kr.R;
import com.example.dllo.my36kr.model.VideoBean;
import com.example.dllo.my36kr.net.SingleVolley;
import com.example.dllo.my36kr.net.VolleyResult;
import com.example.dllo.my36kr.tools.NewsUrls;
import com.example.dllo.my36kr.ui.activity.AbsBaseActivity;
import com.example.dllo.my36kr.ui.adapter.VideoAdapter;
import com.google.gson.Gson;
import java.util.List;
/**
 * Created by CPC_272 on 16/8/25.
 */
public class VideoActivity extends AbsBaseActivity implements View.OnClickListener, VolleyResult {
    private ImageView backTV;
    private VideoView videoView;
    private String videoUrl = NewsUrls.TV_URL;
    private ListView ls_video;
    private ImageView image_video;
    private List<VideoBean.DataBean.DataBean1> dataBean1s;
    @Override
    protected int setLayout() {
        return R.layout.activity_video;
    }
    @Override
    protected void initView() {
        ls_video = byView(R.id.ls_video);
        backTV = byView(R.id.back_ketv);
    }
    @Override
    protected void initData() {
     backTV.setOnClickListener(this);
        SingleVolley.getInstence(this).startRequest(videoUrl,this);
    }

    @Override
    public void onClick(View v) {
        finish();
    }

    @Override
    public void success(String str, int type) {

    }

    @Override
    public void success(String str) {
        Gson gson = new Gson();
        VideoBean videoBean = gson.fromJson(str, VideoBean.class);
        VideoBean.DataBean dataBean = videoBean.getData();
        dataBean1s = dataBean.getData();
        final VideoAdapter videoAdapter = new VideoAdapter(this);
        videoAdapter.setDataBean1s(dataBean1s);

        ls_video.setAdapter(videoAdapter);
        //TV界面行布局点击事件
        // 一定要用适配器中的view和数据 加载控件  parent就是适配器放回的view
        ls_video.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                VideoBean.DataBean.DataBean1 dataBean1 = (VideoBean.DataBean.DataBean1) parent.getItemAtPosition(position);
                videoView = (VideoView) view.findViewById(R.id.videoview);
                image_video = (ImageView) view.findViewById(R.id.img_video);
                videoView.setVisibility(View.VISIBLE);
                videoView.setVideoURI(Uri.parse(dataBean1s.get(position).getTv().getVideoSource360()));
                image_video.setVisibility(View.GONE);
                /**
                 * 其他方法:
                 *videoView.pause();
                 *videoView.stop();
                 *videoView.resume();
                 *videoView.setOnPreparedListener(this);
                 *videoView.setOnErrorListener(this);
                 *videoView.setOnCompletionListener(this);
                 */
                videoView.canPause();
                videoView.start();
                //getTag就是ViewHolder   此行代码  是将当前的ViewHolder的position 赋值给适配器中的PlayPos
                videoAdapter.setVideoHodler((VideoAdapter.VideoHodler) view.getTag())
                        .setPlayPos(position);
            }
        });
    }

    @Override
    public void failure() {

    }
}
